package com.ssm.pre.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.dao.FaultDao;
import com.ssm.pre.service.FaultService;
import com.ssm.pre.util.PageCount;
import com.ssm.pre.util.ToolUtil;
import com.ssm.util.object.InputObject;
import com.ssm.util.object.OutputObject;

@Service
public class FaultServiceImpl implements FaultService {
	
	@Autowired
	private FaultDao faultDao;
	
	/**
	 * 查询故障类型
	 */
	@Override
	public void queryFaultList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = faultDao.queryFaultList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size()); 
	}
	
	/**
	 * 添加故障類型
	 */
	@Override
	public void addFault(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
			
		map.put("createId", inputObject.getLogParams().get("id"));
		map.put("createTime", ToolUtil.getTimeAndToString());
		int num = faultDao.addFault(map);
		outputObject.settotal(num);
		
	}
	
	/**
	 * 刪除故障類型
	 */
	@Override
	public void deleteFaultById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		int num = faultDao.deleteFaultById(params);
		outputObject.settotal(num);
		
	}
	
	/**
	 * 修改
	 */
	@Override
	public void editFault(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		params.put("createId", inputObject.getLogParams().get("id"));
		params.put("createTime", ToolUtil.getTimeAndToString());
		faultDao.editFault(params);
		
		
	}
	
	/**
	 * 查找一条数据
	 */
	@Override
	public void queryFaultOneById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		Map<String, Object> bean = faultDao.findFaultById(map);
		outputObject.setBean(bean);
		
	}

	/**
	 * 查询所有的故障类型名称
	 */
	@Override
	public void queryFaultByName(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = faultDao.queryFaultByName(map);
		outputObject.setBeans(beans);
	}

}
